package com.slwish.xty.utils;

import android.content.Context;
import android.widget.Toast;

import com.slwish.xty.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import static com.slwish.xty.utils.ResourceUtils.getString;

/**
 * 模块说明：
 *
 * @author 陈浩
 * @date 2017/12/25
 */

public class ShareSdkUtils {
    public static final int QQ = 0;
    public static final int SINAWEIBO = 1;
    public static final int WECHAT = 2;
    public static final int WECHAT_MOMENT = 3;

    public static void share(Context mContext) {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // 分享时Notification的图标和文字  2.5.9以后的版本不     调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3860616424,1789830124&fm=80&w=179&h=119&img.PNG");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                String name = platform.getName();
                if ("QQ".equals(name)) {//QQ

                } else if ("SinaWeibo".equals(name)) {//新浪微博

                } else if ("Wechat".equals(name)) {//微信

                } else if ("WechatMoments".equals(name)) {//微信朋友圈

                }
            }
        });
        // 启动分享GUI
        oks.show(mContext);
    }

    public static void share(final Context context, String title, String titleUrl, String text, String imagUrl, int terrace) {

        Platform.ShareParams share = new Platform.ShareParams();
//        share.setShareType(Platform.SHARE_WEBPAGE);
        share.setTitle(title);
        share.setTitleUrl(titleUrl);
        share.setText(text);
        share.setImageUrl(imagUrl);
        share.setUrl(titleUrl);
        share.setShareType(Platform.SHARE_WEBPAGE);
        Platform platform = null;
        switch (terrace) {
            case SINAWEIBO:
                platform = ShareSDK.getPlatform(SinaWeibo.NAME);
                break;
            case QQ:
                platform = ShareSDK.getPlatform(cn.sharesdk.tencent.qq.QQ.NAME);
                break;
            case WECHAT:
                platform = ShareSDK.getPlatform(Wechat.NAME);
                break;
            case WECHAT_MOMENT:
                platform = ShareSDK.getPlatform(WechatMoments.NAME);
                break;
            default:
                break;
        }
        platform.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                Toast.makeText(context, "分享失败",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                Toast.makeText(context, "分享成功",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                Toast.makeText(context, "取消分享",
                        Toast.LENGTH_SHORT).show();
            }
        });
        platform.share(share);
    }
}
